﻿using SD.Common;
using SD.Infrastructure.DTOBase;
using SD.Infrastructure.WPF.Caliburn.Aspects;
using SD.Infrastructure.WPF.Caliburn.Base;
using SD.Infrastructure.WPF.Extensions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.ServiceModel.Extensions;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using SlamDunk.MES.IAppService.DTOs.Outputs.StockContext;
using SlamDunk.MES.IAppService.Interfaces;
using SlamDunk.ValueObjects.Enums;

namespace SlamDunk.MES.Client.ViewModels.StockInOrder
{
    /// <summary>
    /// 入库单据查看ViewModel
    /// </summary>
    public class LookViewModel : ScreenBase
    {
        #region # 字段及构造器
        /// <summary>
        /// 库存单据服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IStockOrderContract> _iStockOrderContract;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public LookViewModel(ServiceProxy<IStockOrderContract> iStockOrderContract)
        {
            this._iStockOrderContract = iStockOrderContract;
        }

        #endregion

        #region 属性
        #region 单据编号
        /// <summary>
        /// 单据编号
        /// </summary>
        [DependencyProperty]
        public string Number { get; set; }
        #endregion

        #region 储存位置类型
        /// <summary>
        /// 储存位置类型
        /// </summary>
        [DependencyProperty]
        public string StorageLocationType { get; set; }
        #endregion

        #region 单据类型
        /// <summary>
        /// 单据类型
        /// </summary>
        [DependencyProperty]
        public string StockOrderClass { get; set; }
        #endregion

        #region 储存位置
        /// <summary>
        /// 储存位置
        /// </summary>
        [DependencyProperty]
        public string StorageLocation { get; set; }
        #endregion

        #region 源单据类型
        /// <summary>
        /// 源单据类型
        /// </summary>
        [DependencyProperty]
        public string SourceOrderType { get; set; }
        #endregion

        #region 源单据信息
        /// <summary>
        /// 源单据信息
        /// </summary>
        [DependencyProperty]
        public string SourceOrder { get; set; }
        #endregion

        #region 描述
        /// <summary>
        /// 描述
        /// </summary>
        [DependencyProperty]
        public string Description { get; set; }
        #endregion

        #region 工厂模型
        /// <summary>
        /// 工厂模型
        /// </summary>
        [DependencyProperty]
        public string FacilityName { get; set; }
        #endregion

        #region 入库单明细列表
        /// <summary>
        /// 入库单明细列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<StockOrderDetailInfo> StockOrderDetailInfoList { get; set; }
        #endregion
        #endregion

        #region 方法
        #region 加载 —— async Task Load(StockInOrderInfo stockInOrderInfo)
        /// <summary>
        /// 加载
        /// </summary>
        public async Task Load(StockInOrderInfo stockInOrderInfo)
        {
            this.Busy();
            this.Number = stockInOrderInfo.Number;
            this.StorageLocationType = EnumExtension.GetEnumMember(stockInOrderInfo.TargetStorageLocationType);
            this.StorageLocation = stockInOrderInfo.TargetStorageLocationName;
            if (stockInOrderInfo.SourceOrderType != null)
            {
                this.SourceOrderType = EnumExtension.GetEnumMember(stockInOrderInfo.SourceOrderType);
            }
            this.SourceOrder = stockInOrderInfo.SourceOrderName;
            this.Description = stockInOrderInfo.Description;
            this.FacilityName = stockInOrderInfo.FacilityInfo?.Name;
            IEnumerable<StockOrderDetailInfo> StockInOrderInfos = await Task.Run(() => this._iStockOrderContract.Channel.GetStockOrderDetails(stockInOrderInfo.Id));
            StockOrderDetailInfoList = new ObservableCollection<StockOrderDetailInfo>(StockInOrderInfos);

            PageModel<StockOrderClassInfo> StockOrderClassInfos = await Task.Run(() => this._iStockOrderContract.Channel.GetStockOrderClassesByPage(null, StockDirectionType.Input, null, stockInOrderInfo.FacilityId, 1, int.MaxValue));
            this.StockOrderClass = StockOrderClassInfos.Datas.Where(x => x.Id == stockInOrderInfo.StockOrderClassId).FirstOrDefault().Name;
            this.Idle();
        }
        #endregion
        #endregion
    }
}
